﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Streambolics.Tablet
{
    public class TouchApi
    {
        public const int WM_TOUCH = 0x0240;
        public const int WM_TOUCHDOWN = 0x0241;
        public const int WM_TOUCHUP = 0x0242;
        public const int WM_GESTURECOMMAND = 0x0243; //Fix

        // Touch event flags ((TOUCHINPUT.dwFlags) [winuser.h]
        public const int TOUCHEVENTF_MOVE = 0x0001;
        public const int TOUCHEVENTF_DOWN = 0x0002;
        public const int TOUCHEVENTF_UP = 0x0004;
        public const int TOUCHEVENTF_INRANGE = 0x0008;
        public const int TOUCHEVENTF_PRIMARY = 0x0010;
        public const int TOUCHEVENTF_NOCOALESCE = 0x0020;
        public const int TOUCHEVENTF_PEN = 0x0040;

        // Touch input mask values (TOUCHINPUT.dwMask) [winuser.h]
        public const int TOUCHINPUTMASKF_TIMEFROMSYSTEM = 0x0001; // the dwTime field contains a system generated value
        public const int TOUCHINPUTMASKF_EXTRAINFO = 0x0002; // the dwExtraInfo field is valid
        public const int TOUCHINPUTMASKF_CONTACTAREA = 0x0004; // the cxContact and cyContact fields are valid

        [StructLayout (LayoutKind.Sequential)]
        public struct TOUCHINPUT
        {
            public int x;
            public int y;
            public System.IntPtr hSource;
            public int dwID;
            public int dwFlags;
            public int dwMask;
            public int dwTime;
            public System.IntPtr dwExtraInfo;
            public int cxContact;
            public int cyContact;
        }

        [StructLayout (LayoutKind.Sequential)]
        public struct POINTS
        {
            public short x;
            public short y;
        }

        [StructLayout (LayoutKind.Sequential)]
        public struct GESTURECOMMAND
        {
            public int cbSize;
            public int dwFlags;
            public int dwCommand;
            public int dwArguments;
            POINTS ptsLocation;
        }

        // Currently touch/multitouch access is done through unmanaged code
        // We must p/invoke into user32 [winuser.h]

        [DllImport ("user32")]
        [return: MarshalAs (UnmanagedType.Bool)]
        public static extern bool RegisterTouchWindow (System.IntPtr hWnd, ulong ulFlags);

        [DllImport ("user32")]
        [return: MarshalAs (UnmanagedType.Bool)]
        public static extern bool GetTouchInputInfo (System.IntPtr hTouchInput, int cInputs, [In, Out] TOUCHINPUT[] pInputs, int cbSize);

        [DllImport ("user32")]
        [return: MarshalAs (UnmanagedType.Bool)]
        public static extern void CloseTouchInputHandle (System.IntPtr lParam);



    }
}
